package org.workplicity.cia.table;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.table.AbstractTableModel;
import org.workplicity.cia.worklet.ElogsWorklet;
import org.workplicity.elog.entry.Elog;
import org.workplicity.task.NetTask;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;
import org.workplicity.entry.User;

/**
 * @author Elog Team
 */

public class SearchResultsTableModel extends AbstractTableModel {

    // Class Variables
    private static ArrayList<Elog> results = new ArrayList<Elog>( );
    private HashMap<Integer,Elog> dirty = new HashMap<Integer,Elog>( );

    private ElogsWorklet worklet = ElogsWorklet.getInstance();
    private WorkletContext context = worklet.getContext();

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("MM / dd / yyyy");
    
    private static String[] columnNames = {
        "Id",
        "Updated",
        "User",
        "Type",
        "Status",
        "Description"
    };
    
    /**
     * Sets the data model data
     * @param elogs 
     */
    public static void setData(ArrayList<Elog> elogs){
        results = elogs;
    }

    /**
     * Gets row size
     * @return row size as int
     */
    public int getRowCount() {
        return results.size();
    }

    /**
     * Gets column count
     * @return column count as int
     */
    public int getColumnCount() {
        return columnNames.length;
    }

    /**
     * Gets name of column
     * @param col
     * @return column name as String
     */
    @Override
    public String getColumnName(int col){
        return columnNames[col];
    }

    /**
     * Returns true if the cell is editable, else returns false
     * @param row
     * @param col
     * @return false (no cells are editable)
     */
    @Override
    public boolean isCellEditable(int row, int col){
        return false;
    }

    /**
     * Gets a row at an index
     * @param row
     * @return the current row (used in editing rows)
     */
     public Elog getRow(int row) {
        if(row >= results.size())
            return null;

        return results.get(row);
    }

     /**
      * Gets data for a cell
      * @param row
      * @param col
      * @return the data for a specific cell
      */
    public Object getValueAt(int row, int col) {
        // get the elog row
        Elog result = results.get(row);

        // id column
        if(col == 0){
            String indicator = "";
            int wsId = result.getId();

            if(dirty.get(wsId) != null){
                indicator = "*";
            }
            
            return wsId + indicator;
        }

        // Updated column
        if(col == 1)
            return dateFormat.format(result.getUpdateDate());

        // User column
        if(col == 2){
            int userId = result.getUpdateUserId();
            String criteria = "/list[id="+userId+"]";
            ArrayList<User> users = Helper.query(NetTask.REPOS_ACCOUNTS, criteria, context);
            return users.get(0).getLogname();
        }

        // Type column
        if(col == 3)
            return result.getType();

        // Status column
        if(col == 4)
            return result.getStatus();

        // Description Column
        if(col == 5){
            return result.getDescription();
        }
        
        // non-existant column
        return "?";
    }
}